﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Cs期中作业
{
    public partial class 删除学生 : Form
    {
        public 删除学生()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder ipS = new StringBuilder();
            StringBuilder dbS = new StringBuilder();
            StringBuilder stuShS = new StringBuilder();
            StringBuilder sqlUserS = new StringBuilder();
            StringBuilder sqlPwS = new StringBuilder();
            Gen.GetPrivateProfileString("Host", "Server", "localhost", ipS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "Database", "Dorm", dbS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "StuSheet", "Student", stuShS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "UserName", "", sqlUserS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "Password", "", sqlPwS, 255, @".\config.ini");
            string ip = ipS.ToString();
            string db = dbS.ToString();
            string stuSh = stuShS.ToString();
            string sqlUser = sqlUserS.ToString();
            string sqlPw = sqlPwS.ToString();
            string connString = "Data Source=" + ip + ";Initial Catalog=" + db + ";Persist Security Info=True;User ID=" + sqlUser + ";Password=" + sqlPw;
            SqlConnection connection = new SqlConnection(connString);
            string sql = "delete from " + stuSh + " where 学号like'" + textBox1.Text + "'and 班级like'" + textBox2.Text + "'";
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand(sql, connection);
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("删除学生信息成功", "删除成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("删除学生信息失败¹", "删除失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "操作数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                connection.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
